-- UIStrongBox
-- Created by cheny3 Sep/15/2015
-- 保险箱建筑

UIStrongBox = class("UIStrongBox", function()
    return cc.Layer:create();
end);

function UIStrongBox.create(gridData, viewFunc, iconOffsetY, dialog, title, iconPath)
    return UIStrongBox.new(gridData, viewFunc, iconOffsetY, dialog,  title, iconPath);
end

-- 按钮个数
local BTN_COUNT = 6;

function UIStrongBox:ctor(gridData, viewFunc, iconOffsetY, dialog, title, iconPath)
    -- 初始化
    self:setName("UIStrongBox");
    local node = cc.CSLoader:createNode("layout/dungeon/special/StrongBox.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    assert(type(gridData) == "table", "必须传入格子数据");

    self.gridData  = gridData;

    -- 查询建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    self.title     = title or buildingInfo["title"];                            -- 标题
    self.iconPath  = iconPath or getLevelItemIconPath(buildingInfo["icon"]);    -- 图标路径
    self.dialog    = dialog or BuildingBonusM.getBuildingDialog(self.gridData); -- 对话内容
    self.iconOffsetY = iconOffsetY or 0;                                        -- 图标纵向偏移量
    self.viewFunc  = viewFunc;                                                  -- 回调函数
    self.click     = {};                                                        -- 按密码顺序

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIStrongBox:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIStrongBox:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIStrongBox 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIStrongBox:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIStrongBox");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 获取按钮上的数字
    local numbers = FormulaM.invoke("STRONG_BOX_PASSWORD", self.gridData.type);
    table.sort(numbers);

    -- 注册数字按钮点击事件
    for i = 1, BTN_COUNT do
        local btn = findChildByName(node, "CT/btn"..i);
        local function onOkClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_select");
                table.insert(self.click, numbers[i]);
                btn:setTouchEnabled(false);
                btn:setBright(false);

                -- 是否符合密码顺序
                if #self.click == BTN_COUNT then
                    -- 尝试解锁保险箱
                    self:tryUnlockBox();
                end
            end
        end
        AddTouchEventListener(btn, onOkClick);
        TextStyleM.setTextStyle(btn, TextStyleM.TEXT_SIZE_MIDDLE2, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
        btn:setTitleText(tostring(numbers[i]));
    end
end

-- 重绘
function UIStrongBox:redraw()
    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");
    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    titleLabel:setString(self.title);
    iconImg:loadTexture(self.iconPath);
    iconImg:setPositionY(iconImg:getPositionY() + self.iconOffsetY);
    local iconSize = iconImg:getContentSize();

    iconImg:setScale(1.3);
    TextStyleM.setArtWordTitleStyle(titleLabel);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);

    applyString(descLabel, self.dialog);
end

-- 尝试打开保险箱
function UIStrongBox:tryUnlockBox()
    -- 保险箱密码
    local password = FormulaM.invoke("STRONG_BOX_PASSWORD", self.gridData.type);
    local canOpen = true;

    -- 判断密码正确性
    for i = 1, BTN_COUNT do
        if self.click[i] ~= password[i] then
            canOpen = false;
            break;
        end
    end

    if canOpen then
        -- 飘字提示
        alert(getLocStr("strong_box_open"));
        -- 奖励回调
        self.viewFunc();
        -- 关闭当前界面
        UIMgr.getCurrentScene():removeFormByName("UIStrongBox");
    else
        -- 关闭当前界面
        --UIMgr.getCurrentScene():removeFormByName("UIStrongBox");
        -- 飘字提示
        alert(getLocStr("strong_box_no_open"));
        -- 按钮重置
        self.click = {};
        for i = 1, BTN_COUNT do
            local btn = findChildByName(self.node, "CT/btn"..i);
            btn:setTouchEnabled(true);
            btn:setBright(true);
        end
    end
end








